//
//  ByLotteryAlgorithmHandle.m
//  caipiao_ios
//
//  Created by mc on 2018/7/13.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByLotteryAlgorithmHandle.h"

@implementation ByLotteryAlgorithmHandle

//初始化数据
+ (NSMutableArray *)getOriginalDataWithCount:(NSInteger)count{
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
    for (NSInteger i = 0; i < count; i++) {
        ByNetModel *netModel = [[ByNetModel alloc] init];
        netModel.value = i + 1;
        netModel.selected = NO;
        [dataArray addObject:netModel];
    }
    return dataArray;
}

+ (NSMutableArray *)getSelectedDataWithArray:(NSArray *)array{
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    for (ByNetModel *netModel in array) {
        if (netModel.selected) {
            [tempArray addObject:netModel];
        }
    }
    return tempArray;
}

+ (NSMutableArray *)getRandomAllDataWithCount:(NSInteger)count selectedCount:(NSInteger)selectedCount{
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *randomArray = [NSMutableArray arrayWithCapacity:1];
    while (randomArray.count < selectedCount) {
        NSInteger x = arc4random() % count;
        NSString *index = [NSString stringWithFormat:@"%ld", x];
        if (![randomArray containsObject:index]) {
            [randomArray addObject:index];
        }
    }
    
    tempArray = [self getOriginalDataWithCount:count];
    
    for (NSInteger i = 0; i < count; i++) {
        ByNetModel *netModel = [tempArray objectAtIndex:i];
        for (NSInteger j = 0; j < randomArray.count; j++) {
            NSInteger index = [[randomArray objectAtIndex:j]  integerValue];
            if (index == i) {
                netModel.selected = YES;
            }
        }
    }
    
    return tempArray;
    
}

//机选
+ (NSMutableArray *)getRandomSelectedDataWithCount:(NSInteger)count selectedCount:(NSInteger)selectedCount color:(UIColor *)color{
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    
    NSMutableArray *randomArray = [NSMutableArray arrayWithCapacity:1];
    while (randomArray.count < selectedCount) {
       NSInteger x = arc4random() % count;
        NSString *index = [NSString stringWithFormat:@"%ld", x];
        if (![randomArray containsObject:index]) {
            [randomArray addObject:index];
        }
    }
    
    tempArray = [self getOriginalDataWithCount:count];
    
    NSMutableArray *selectedArray = [NSMutableArray arrayWithCapacity:1];
    for (NSInteger i = 0; i < count; i++) {
        ByNetModel *netModel = [tempArray objectAtIndex:i];
        for (NSInteger j = 0; j < randomArray.count; j++) {
            NSInteger index = [[randomArray objectAtIndex:j]  integerValue];
            if (index == i) {
                netModel.selected = YES;
                netModel.themeColor = color;
                [selectedArray addObject:netModel];
            }
        }
    }
    return selectedArray;
}

//机选大乐透
+ (NSMutableArray *)getRandomSuperSelectedData{
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *redArray = [self getRandomSelectedDataWithCount:35 selectedCount:5 color:BY_THEME_COLOR];
    NSMutableArray *blueArray = [self getRandomSelectedDataWithCount:12 selectedCount:2 color:BY_Blue_COLOR];
    
    [tempArray addObjectsFromArray:[redArray copy]];
    [tempArray addObjectsFromArray:[blueArray copy]];
    
    return tempArray;
}

//机选大乐透 红球 篮球分开
+ (NSMutableArray *)getRandomSuperSelectedDataTwoArray{
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *redArray = [self getRandomSelectedDataWithCount:35 selectedCount:5 color:BY_THEME_COLOR];
    NSMutableArray *blueArray = [self getRandomSelectedDataWithCount:12 selectedCount:2 color:BY_Blue_COLOR];
    
    [tempArray addObject:redArray];
    [tempArray addObject:blueArray];
    return tempArray;
}

+ (NSMutableArray *)getRandomDoubleBallSelectedDataTwoArray{
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *redArray = [self getRandomSelectedDataWithCount:33 selectedCount:6 color:BY_THEME_COLOR];
    NSMutableArray *blueArray = [self getRandomSelectedDataWithCount:16 selectedCount:1 color:BY_Blue_COLOR];
    
    [tempArray addObject:redArray];
    [tempArray addObject:blueArray];
    return tempArray;
}


//机选双色球
+ (NSMutableArray *)getRandomDoubleBallSelectedData{
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *redArray = [self getRandomSelectedDataWithCount:33 selectedCount:6 color:BY_THEME_COLOR];
    NSMutableArray *blueArray = [self getRandomSelectedDataWithCount:16 selectedCount:1 color:BY_Blue_COLOR];
    
    [tempArray addObjectsFromArray:[redArray copy]];
    [tempArray addObjectsFromArray:[blueArray copy]];
    
    return tempArray;
}

//注数计算
+ (NSInteger)getCountWithSelectedData:(NSMutableArray *)selectedAry leastNumber:(NSInteger)number{
    
    NSMutableArray *tempArray = [self getSelectedDataWithArray:selectedAry];

    NSInteger value = number;
    for (NSInteger i = number - 1; i > 1 ; i--) {
        value = value * i;
    }
    
    NSInteger count = tempArray.count;
    NSInteger total = count;
   
    for (NSInteger i = count - 1; i > count - number; i--) {
        total = total * i;
    }
    return total / value;
    
}

//机选所有数据
+ (NSMutableArray *)getRandomAllDataWithCount:(NSInteger)count oneSelectedCount:(NSInteger)selectedCount anotherSelectedCount:(NSInteger )anotherCount{
    
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *array2 = [NSMutableArray arrayWithCapacity:1];
    
    NSMutableArray *randomArray = [NSMutableArray arrayWithCapacity:1];
    while (randomArray.count < selectedCount) {
        NSInteger x = arc4random() % count;
        NSString *index = [NSString stringWithFormat:@"%ld", x];
        if (![randomArray containsObject:index]) {
            [randomArray addObject:index];
        }
    }
    array1 = [self getOriginalDataWithCount:count];
    array2 = [self getOriginalDataWithCount:count];
    for (NSInteger i = 0; i < count; i++) {
        ByNetModel *netModel = [array1 objectAtIndex:i];
        for (NSInteger j = 0; j < randomArray.count; j++) {
            NSInteger index = [[randomArray objectAtIndex:j]  integerValue];
            if (index == i) {
                netModel.selected = YES;
            }
        }
    }
    
    if (anotherCount == 0) {
        return array1;
    }
    
    NSMutableArray *otherRandomArray = [NSMutableArray arrayWithCapacity:1];
    while (otherRandomArray.count < anotherCount) {
        NSInteger x = arc4random() % count;
        NSString *index = [NSString stringWithFormat:@"%ld", x];
        if (![otherRandomArray containsObject:index] && ![randomArray containsObject:index]) {
            [otherRandomArray addObject:index];
        }
    }
    
    for (NSInteger i = 0; i < count; i++) {
        ByNetModel *netModel = [array2 objectAtIndex:i];
        for (NSInteger j = 0; j < otherRandomArray.count; j++) {
            NSInteger index = [[otherRandomArray objectAtIndex:j]  integerValue];
            if (index == i) {
                netModel.selected = YES;
            }
        }
    }
    
    [tempArray addObject:array1];
    [tempArray addObject:array2];
    return tempArray;
}


//获取数字彩票 单号num 字符串
+ (NSMutableArray *)getNumArrayWithDataArray:(NSMutableArray *)dataArray{
    
    NSMutableArray *numArray = [NSMutableArray arrayWithCapacity:1];
    
    for (int i = 0; i < dataArray.count; i++){
        
        ByNetModel *netModel = [dataArray objectAtIndex:i];
        NSString *string = @"";
        if (netModel.dataArray.count == 2) {
            
            NSMutableArray *redArray = [netModel.dataArray firstObject];
            NSMutableArray *blueArray = [netModel.dataArray lastObject];
            
            NSMutableArray *redValueArray = [self getValueArrayWithModelArray:redArray];
            NSMutableArray *blueValueArray = [self getValueArrayWithModelArray:blueArray];
            
            NSString *redString = [redValueArray componentsJoinedByString:@","];
            NSString *blueString = [blueValueArray componentsJoinedByString:@","];
            
            string = [NSString stringWithFormat:@"%@|%@", redString,blueString];
            
        }else if (netModel.dataArray.count == 4){
            
            NSMutableArray *red1Array = [netModel.dataArray objectAtIndex:0];
            NSMutableArray *red2Array = [netModel.dataArray objectAtIndex:1];
            
            NSMutableArray *blue1Array = [netModel.dataArray objectAtIndex:2];
            NSMutableArray *blue2Array = [netModel.dataArray objectAtIndex:3];
            
            
            NSMutableArray *red1ValueArray = [self getValueArrayWithModelArray:red1Array];
            NSMutableArray *red2ValueArray = [self getValueArrayWithModelArray:red2Array];
            
            NSMutableArray *blue1ValueArray = [self getValueArrayWithModelArray:blue1Array];
            NSMutableArray *blue2ValueArray = [self getValueArrayWithModelArray:blue2Array];
            
            NSString *red1String = [red1ValueArray componentsJoinedByString:@","];
            NSString *red2String = [red2ValueArray componentsJoinedByString:@","];
            NSString *blue1Stirng = [blue1ValueArray componentsJoinedByString:@","];
            NSString *blue2String = [blue2ValueArray componentsJoinedByString:@","];
            
            string = [NSString stringWithFormat:@"%@#%@|%@#%@", red1String,red2String,blue1Stirng,blue2String];
            
            
        }
        
        NSLog(@"num = %@", string);
        [numArray addObject:string];
    }
    return numArray;
    
}


//得到单注彩票
+ (NSMutableArray *)getSingleLotteryWithArray:(NSMutableArray *)array{
    
    NSMutableArray *numArray = [NSMutableArray arrayWithCapacity:1];
    
    NSMutableArray *redArray = [NSMutableArray array];
    NSMutableArray *blueArray = [NSMutableArray array];
    
    
    for (int i = 0; i < array.count; i++){
        
        ByNetModel *netModel = [array objectAtIndex:i];
        if ([netModel.themeColor isEqual:BY_THEME_COLOR]) {
            [redArray addObject:[NSString stringWithFormat:@"%ld", netModel.value]];
        }else{
            [blueArray addObject:[NSString stringWithFormat:@"%ld", netModel.value]];
        }

       
    }
    
    NSString *redString = [redArray componentsJoinedByString:@","];
    NSString *blueString = [blueArray componentsJoinedByString:@","];
    
    NSString * string = [NSString stringWithFormat:@"%@|%@", redString,blueString];
    [numArray addObject:string];
    return numArray;
    
}


//获取双色球 单号num 字符串
+ (NSMutableArray *)getColorBallNumArrayWithDataArray:(NSMutableArray *)dataArray{
    
    NSMutableArray *numArray = [NSMutableArray arrayWithCapacity:1];
    
    for (int i = 0; i < dataArray.count; i++){
        
        ByNetModel *netModel = [dataArray objectAtIndex:i];
        NSString *string = @"";
        if (netModel.dataArray.count == 2) {
            
            NSMutableArray *redArray = [netModel.dataArray firstObject];
            NSMutableArray *blueArray = [netModel.dataArray lastObject];
            
            NSMutableArray *redValueArray = [self getValueArrayWithModelArray:redArray];
            NSMutableArray *blueValueArray = [self getValueArrayWithModelArray:blueArray];
            
            NSString *redString = [redValueArray componentsJoinedByString:@","];
            NSString *blueString = [blueValueArray componentsJoinedByString:@","];
            
            string = [NSString stringWithFormat:@"%@|%@", redString,blueString];
            
        }else if (netModel.dataArray.count == 3){
            
            NSMutableArray *red1Array = [netModel.dataArray objectAtIndex:0];
            NSMutableArray *red2Array = [netModel.dataArray objectAtIndex:1];
            NSMutableArray *blueArray = [netModel.dataArray objectAtIndex:2];
           

            NSMutableArray *red1ValueArray = [self getValueArrayWithModelArray:red1Array];
            NSMutableArray *red2ValueArray = [self getValueArrayWithModelArray:red2Array];
            NSMutableArray *blueValueArray = [self getValueArrayWithModelArray:blueArray];
           
            
            NSString *red1String = [red1ValueArray componentsJoinedByString:@","];
            NSString *red2String = [red2ValueArray componentsJoinedByString:@","];
            NSString *blueStirng = [blueValueArray componentsJoinedByString:@","];
            string = [NSString stringWithFormat:@"%@#%@|%@", red1String,red2String,blueStirng];
            
        }
        
        NSLog(@"num = %@", string);
        [numArray addObject:string];
    }
    return numArray;
    
    
}

+ (NSMutableArray *)getValueArrayWithModelArray:(NSMutableArray *)modelsArray{
    NSMutableArray *valuesArray = [NSMutableArray arrayWithCapacity:1];
    for (ByNetModel *netModel in modelsArray) {
        [valuesArray addObject:[NSString stringWithFormat:@"%ld", netModel.value]];
    }
    return valuesArray;
}


//由num 获取Model
+ (NSMutableArray *)getModelsArrayWithNumString:(NSString *)num{
    
    NSMutableArray *modelsArray = [NSMutableArray arrayWithCapacity:1];
    
    NSArray *itemsArray = [num componentsSeparatedByString:@"|"];
    if (itemsArray.count == 2) {
        NSString *redString = [itemsArray firstObject];
        NSString *blueString = [itemsArray lastObject];
        
        NSArray *redNumArray = [redString componentsSeparatedByString:@","];
        NSArray *blueNumArray = [blueString componentsSeparatedByString:@","];
        for (NSNumber *number in redNumArray) {
            ByNetModel *netModel = [[ByNetModel alloc] init];
            netModel.value = number.integerValue;
            netModel.title = [NSString stringWithFormat:@"%@", number];
            netModel.themeColor = BY_THEME_COLOR;
            [modelsArray addObject:netModel];
        }
        
        for (NSNumber *number in blueNumArray) {
            ByNetModel *netModel = [[ByNetModel alloc] init];
            netModel.value = number.integerValue;
            netModel.themeColor = BY_Blue_COLOR;
            netModel.title = [NSString stringWithFormat:@"%@", number];
            [modelsArray addObject:netModel];
        }
        
        return modelsArray;
        
    }else{
        return [NSMutableArray new];
    }
    
    
    
}


+ (NSMutableArray *)getModelsArrayWithArray:(NSArray *)array WithColor:(UIColor *)color{
    NSMutableArray *modelsArray = [NSMutableArray arrayWithCapacity:1];
    for (NSString *str in array) {
        ByNetModel *netModel = [[ByNetModel alloc] init];
        netModel.title = str;
        netModel.themeColor = color;
        [modelsArray addObject:netModel];
    }
    return modelsArray;
}


@end
